﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Threading;
using System.Diagnostics;

namespace PHPFileChecker
{
    struct Config
    {
        public const int MAX_TICKET = 10;
    }

    struct WorkOption
    {
        public string Path;
        public List<string> FileType;
        public bool CheckBOM;
        public bool ClearBOM;
        public bool CheckWhiteSpace;
        public bool ClearWhiteSpace;
        public bool CheckUTF8;
        public bool BackupFile;
    }

    delegate void Notify_Delegate(string info);

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = Application.StartupPath;
            textBox2.Text = ".php|.html|.htm";
            foreach (Control c in this.Controls)
            {
                if (c is CheckBox)
                {
                    toolTip1.SetToolTip(c, ((CheckBox)c).Text);
                }
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            button2.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            WorkOption wo;
            wo.Path = textBox1.Text.Trim();
            wo.FileType = new List<string>();
            wo.CheckBOM = checkBox1.Checked;
            wo.ClearBOM = checkBox2.Checked;
            wo.CheckWhiteSpace = checkBox3.Checked;
            wo.ClearWhiteSpace = checkBox4.Checked;
            wo.CheckUTF8 = checkBox5.Checked;
            wo.BackupFile = checkBox6.Checked;
            foreach (string s in textBox2.Text.Trim().Trim('|').ToLower().Split('|'))
            {
                wo.FileType.Add(s);
            }

            if (string.IsNullOrEmpty(wo.Path))
            {
                MessageBox.Show("请选择目录");
                return;
            }
            if (wo.Path.EndsWith("\\"))
            {
                wo.Path = wo.Path.TrimEnd('\\');
            }
            if (!Directory.Exists(wo.Path))
            {
                MessageBox.Show("目录不存在");
                return;
            }

            button2.Enabled = false;
            toolStripStatusLabel1.Text = "开始检查...";
            listBox1.Items.Clear();

            Worker worker = new Worker();
            worker.OnCheckShow += (info1) =>
            {
                this.BeginInvoke(new Notify_Delegate((info2) =>
                {
                    toolStripStatusLabel1.Text = info2;
                }),
                info1);
            };
            worker.OnCheckResult += (info1) =>
            {
                this.BeginInvoke(new Notify_Delegate((info2) =>
                {
                    listBox1.Items.Add(info2);
                    listBox1.TopIndex = listBox1.Items.Count - (int)(listBox1.Height / listBox1.ItemHeight);
                }),
                info1);
            };
            worker.OnCheckComplete += (info1) =>
            {
                this.BeginInvoke(new Notify_Delegate((info2) =>
                {
                    button2.Enabled = true;
                    toolStripStatusLabel1.Text = info2;
                }),
                info1);
            };

            Thread th = new Thread(new ParameterizedThreadStart(worker.startCheck));
            th.Name = "startCheck";
            th.IsBackground = true;
            th.Start(wo);
        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.C && listBox1.SelectedItems.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                {
                    sb.Append(listBox1.SelectedItems[i]);
                    if (i < listBox1.SelectedItems.Count - 1)
                    {
                        sb.Append("\r\n");
                    }
                }
                Clipboard.Clear();
                Clipboard.SetText(sb.ToString());
            }
            if (e.Control && e.KeyCode == Keys.A)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                    listBox1.SetSelected(i, true);
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                checkBox2.Checked = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                checkBox1.Checked = true;
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox3.Checked)
            {
                checkBox4.Checked = false;
            }
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                checkBox3.Checked = true;
            }
        }
    }
}
